<?php

namespace App\Http\Controllers\Wechat;


use App\Model\Marketing\AboutUs;
use App\Model\Wechat\WechatSubscribe;
use App\Model\Wechat\WechatUser;
use EasyWeChat\Message\News;


class ServerController extends HelpController
{
    protected $message; //信息实例

    protected $openId; //用户openId


    /**
     * 接收消息入口
     * @param
     * @return
     */
    public function server()
    {
        try{
            $server = $this->app->server;
            $server->setMessageHandler(function ($message) {
                $this->message = $message;
                $this->openId = $message->FromUserName;
                switch ($message->MsgType) {
                    case 'event':
                        switch ($message->Event) {
                            case 'subscribe':
                                return $this->introChimp();
                                break;
                            case 'unsubscribe':
                                return $this->removeOpenid();
                                break;
                            default:
                                return '收到事件消息';
                                break;
                        }
                        break;
                    case 'text':
                        return $this->returnMessage();
                        break;
                    // ... 其它消息
                    default:
                        return "您好！欢迎关注SchooIn微信测试号!";
                        break;
                }

            });
            $response = $server->serve();
            // 将响应输出
            return $response; // Laravel 里请使用：return $response;

        }catch (\Exception $e){
            return $this->fail(false,"接收异常".$e->getMessage());
        }
    }

    /**
     * 文字消息请求的处理
     * @param $message
     * @return News|string
     *
     */
    protected function returnMessage()
    {
        switch ($this->message->Content)
        {
            case '1':
                return $this->textMessage('你的OPENID为：'.$this->openId);
                break;
            case '2':
                return $this->textMessage('发送时间为：'.date('Y-m-d H:i:s',$this->message->CreateTime));
                break;
            case '3':
                return $this->introChimp();
                break;
            default:
                return $this->textMessage('Hello, 欢迎访问 SchooIn 微信测试号！');
                break;
        }
    }

    /**
     * 返回Classchimp介绍信息
     * @return News
     */
    protected function introChimp()
    {
        $data = AboutUs::find(1);
        if($data){
            $data['image'] = $data['img'];
            $data['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/about_m.html';
            return $this->newsMessage($data);
        }else{
            $data = WechatSubscribe::find(1);
            if($data){
                return $this->newsMessage($data);
            }else{
                return $this->textMessage('Hello, 欢迎访问 SchooIn 微信测试号！');
            }
        }

    }

    /**
     * 解除openid绑定
     *
     */
    protected function removeOpenid()
    {
        $wechatuser = WechatUser::where('openid',$this->openId)->delete();
    }

}
